package CAS;

/**
 * @program: javase
 * @description: 使用CAS模模拟不可重入锁测试
 * @author: luolidong
 * @create: 2021-06-16 16:34
 * @version: 1.0
 */
public class NoReentrantLockTest {
    public static void main(String[] args) {
        new Thread(new NoReentrantLock()).start();
    }
}
class NoReentrantLock implements Runnable{

    private final NoReentrantLockByCAS lock=new NoReentrantLockByCAS();

    @Override
    public void run() {
        method1();
    }

    public void method1(){
        try {
            lock.lock();
            System.out.println("method1"+Thread.currentThread().getName());
            method2();
            System.out.println("method2结束"+Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private void method2() {
        try {
            lock.lock();
            System.out.println("method2"+Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
